package com.huluobo.a2307monthdemo.videos

import android.animation.Animator
import android.animation.AnimatorListenerAdapter
import android.animation.ObjectAnimator
import android.os.Bundle
import android.util.Log
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.AccelerateInterpolator
import android.view.animation.Animation
import android.view.animation.Animation.AnimationListener
import android.view.animation.AnimationUtils
import android.view.animation.BounceInterpolator
import android.widget.ImageView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.PagerSnapHelper
import androidx.recyclerview.widget.RecyclerView
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.huluobo.a2307monthdemo.R
import com.huluobo.a2307monthdemo.adapter.CommentAdapter
import com.huluobo.a2307monthdemo.adapter.VideoAdapter
import com.huluobo.a2307monthdemo.databinding.FragmentVideosBinding
import com.huluobo.a2307monthdemo.login.LoginActivity
import com.huluobo.lib_base.entity.VideoBean
import com.huluobo.lib_base.mvvm.BaseFragment
import com.huluobo.lib_base.net.Const
import com.shuyu.gsyvideoplayer.video.StandardGSYVideoPlayer


class VideosFragment : BaseFragment<FragmentVideosBinding, VideoViewModel>() {
    private lateinit var videoAdapter: VideoAdapter
    private lateinit var commentAdapter: CommentAdapter

    private var currentPosition = 0
    private lateinit var player: StandardGSYVideoPlayer
    override fun initData() {
        vm.getVideos(2, 10)
        vm.getVideoSuccess.observe(this) {
            videoAdapter.data.clear()
            videoAdapter.data.addAll(it)
            videoAdapter.notifyDataSetChanged()
        }

        vm.likeVideoSuccess.observe(this) {
            ToastUtils.showLong(it)
            videoAdapter.data[currentPosition].dianzan = 1
            videoAdapter.notifyItemChanged(currentPosition)
        }

        vm.unLikeVideoSuccess.observe(this) {
            ToastUtils.showLong(it)
            videoAdapter.data[currentPosition].dianzan = 0
            videoAdapter.notifyItemChanged(currentPosition)
        }

        vm.followAuthSuccess.observe(this) {
            ToastUtils.showLong(it)
            videoAdapter.data[currentPosition].guanzhu = 1
            videoAdapter.notifyItemChanged(currentPosition)
        }

        vm.getCommentSuccess.observe(this) {
            commentAdapter.data.clear()
            commentAdapter.data.addAll(it)
            commentAdapter.notifyDataSetChanged()
        }
    }

    override fun initView() {
        val linearLayouManager = LinearLayoutManager(requireContext())
        binding.videoRv.layoutManager = linearLayouManager
        videoAdapter = VideoAdapter()
        binding.videoRv.adapter = videoAdapter

        val pageSnapHelper = PagerSnapHelper()
        pageSnapHelper.attachToRecyclerView(binding.videoRv)

        binding.videoRv.addOnScrollListener(object : RecyclerView.OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                super.onScrolled(recyclerView, dx, dy)
                currentPosition = linearLayouManager.findFirstVisibleItemPosition()
                player =
                    videoAdapter.getViewByPosition(currentPosition, R.id.item_videos_player) as StandardGSYVideoPlayer
//                player.startPlayLogic()
            }
        })

        videoAdapter.addChildClickViewIds(
            R.id.item_videos_like_iv,
            R.id.item_videos_add_iv,
            R.id.item_videos_close_iv,
            R.id.item_videos_comment_iv
        )
        videoAdapter.setOnItemChildClickListener { adapter, view, position ->
            if (SPUtils.getInstance().getString(Const.PARAM_TOKEN).isNullOrEmpty()) {
                startAct(LoginActivity::class.java)
            } else {
                val videoBean = videoAdapter.data[position]
                when (view.id) {
                    R.id.item_videos_like_iv -> likeVideo(videoBean)
                    R.id.item_videos_add_iv -> followAuth(videoBean)
                    R.id.item_videos_close_iv -> closeComment()
                    R.id.item_videos_comment_iv -> openComment(videoBean)
                }
            }
        }
    }

    private fun openComment(videoBean: VideoBean) {
        binding.videoRv.setIntercept(false)
        val commentLayout = videoAdapter.getViewByPosition(currentPosition, R.id.item_videos_comment_ll)
        if (commentLayout != null) {
            commentLayout.visibility = View.VISIBLE
            val commentRv = videoAdapter.getViewByPosition(currentPosition, R.id.item_videos_comment_rv) as RecyclerView
            commentRv.layoutManager = LinearLayoutManager(requireContext())
            commentAdapter = CommentAdapter()
            commentRv.adapter = commentAdapter

            val animIn = AnimationUtils.loadAnimation(requireContext(), R.anim.anim_in)
            animIn.duration = 500
            animIn.interpolator = BounceInterpolator()
            animIn.setAnimationListener(object : AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                    vm.getComment(videoBean.id)
                }

                override fun onAnimationEnd(animation: Animation?) {
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }
            })
            commentLayout.startAnimation(animIn)
        }
    }

    private fun closeComment() {
        val commentLayout = videoAdapter.getViewByPosition(currentPosition, R.id.item_videos_comment_ll)
        if (commentLayout != null) {
            val animOut = AnimationUtils.loadAnimation(requireContext(), R.anim.anim_out)
            animOut.duration = 500
            animOut.interpolator = AccelerateInterpolator()
            animOut.setAnimationListener(object : AnimationListener {
                override fun onAnimationStart(animation: Animation?) {
                }

                override fun onAnimationEnd(animation: Animation?) {
                    commentLayout.visibility = View.GONE
                    binding.videoRv.setIntercept(true)
                }

                override fun onAnimationRepeat(animation: Animation?) {
                }
            })
            commentLayout.startAnimation(animOut)
        }
    }

    private fun followAuth(videoBean: VideoBean) {
        val map = mapOf(
            Const.PARAM_AUTH_NAME to videoBean.authname
        )
        vm.followAuth(map)
    }

    private fun likeVideo(videoBean: VideoBean) {
        if (videoBean.dianzan == 0) {
            //点赞
            vm.likeVideo(videoBean.id)
        } else {
            //取消点赞
            vm.unLikeVideo(videoBean.id)
        }
    }
}